#include<iostream>
#include<vector>
using namespace std;

struct ListNode {
	int val;
	ListNode *next;
	ListNode() : val(0), next(nullptr) {}
	ListNode(int x) : val(x), next(nullptr) {}
	ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
	ListNode* removeNthFromEnd(ListNode* head, int n) {
		ListNode *t = head;
		vector<ListNode *> list;
		while (t) {
			list.push_back(t);
			t = t->next;
		}
		int index = list.size()-n;
		if (index == 0) 
			head = head->next;
		
		else 
			list[index - 1]->next = list[index]->next;
		return head;
	}
};

void main() {
	ListNode n1(1), n2(2), n3(3), n4(4), n5(5);
	n1.next = &n2, n2.next = &n3, n3.next = &n4, n4.next = &n5;
	Solution s;
	s.removeNthFromEnd(&n1,6);
}